import { Module } from 'vuex'
import { ordersModuleType, GlobalDataProps, ordersListType } from '@/store/storeType'
import { getFn, putFn } from '../store_actions_axiosFn'

const ordersModule: Module<ordersModuleType, GlobalDataProps> = {
  state: () => ({
    ordersObj: {
      orders: []
    },
    expressData: []
  }),
  mutations: {
    getOrdersList(state, data) {
      state.ordersObj = data.data.data
    },
    putChangeOrders(state, data) {
      if (data.status !== 200) return
      state.ordersObj.orders = state.ordersObj.orders.map((item: ordersListType) => {
        if (item.order_id === data.data.data.order_id) item = data.data.data
        return item
      })
    },
    getExpressList(state, data) {
      if (data.status !== 200) return
      state.expressData = data.data.data
    }
  },
  actions: {
    getOrdersList ({ commit }, params) {
      return getFn('orders', 'getOrdersList', commit, params)
    },
    putChangeOrders ({ commit }, putDataAndId) {
      return putFn(`orders/${putDataAndId.order_id}`, putDataAndId.putData, 'putChangeOrders', commit)
    },
    getExpressList ({ commit }, id) {
      return getFn(`express/${id}`, 'getExpressList', commit)
    }
  }
}

export default ordersModule